<template>
  <div class="uploadImg">
    <!-- :http-request="httpRequest" 自定义上传  -->
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img
        v-if="imageUrl"
        v-imgError="defaultImage"
        :src="imageUrl"
        class="avatar"
        @click.stop="showBigImage"
      >
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <span v-if="imageUrl" class="close" @click="closeEvent">X</span>

    <!-- 预览图片 -->
    <el-dialog :visible.sync="isShow" title="图片预览">
      <div style="text-align: center">
        <img class="img" :src="imageUrl" alt="">
      </div>
    </el-dialog>
    <!-- 加入进度条 -->
    <transition name="xxx">
      <el-progress v-if="showProgress" :percentage="percentage" />
    </transition>
  </div>
</template>

<script>
import Cos from 'cos-js-sdk-v5'
const cos = new Cos({
  // 密钥
  SecretId: 'AKIDaz81e3C5i49yWpse9PO5VfYAVZNuA8rK',
  SecretKey: 'D9qGPHsCG3ZppY0DBsIRJuFJ6h14rOdb'
})
export default {
  name: 'UploadImg',
  model: {
    prop: 'imageUrl'
  },
  props: {
    imageUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 通过model
      // imageUrl: ''
      percentage: 0,
      isShow: false,
      showProgress: false,
      defaultImage: require('@/assets/common/head.jpg')
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 1

      if (!isPNG) {
        this.$message.error('上传头像图片只能是 isPNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isPNG && isLt2M
    },
    httpRequest(res) {
      this.showProgress = true
      this.percentage = 0
      // res.file上传文件对象
      console.log(res.data)
      cos.putObject(
        {
          Bucket: 'zxx-1308460152', // 存储通
          Region: 'ap-beijing', // 存储通所在的地域，必须是字段
          Key: res.file.uid + '', // 必须是字符串
          StoreClass: 'STANDARD',
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            // 上传进度
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.showProgress = false
              }, 1000)
            }
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          // 上传成功或者失败的问题
          console.log('err||data', err || data)
          console.log('this', this)
          // data.Location
          // zxx-1308460152.cos.ap-beijing.myqcloud.com/1637652971453
          // this.imageUrl ='http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
        }
      )
    },
    closeEvent() {
      this.$emit('input', '')
    },
    showBigImage() {
      this.isShow = true
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.uploadImg {
  position: relative;
  width: 178px;
  &:hover {
    .close {
      display: block;
    }
  }
  .close {
    display: none;
    position: absolute;
    top: 5px;
    right: 10px;
    cursor: pointer;
  }
  .img {
    width: 100%;
  }
}
.xxx-enter-active,
.xxx-leave-active {
  transition: all 0.5s;
}
.xxx-enter,
.xxx-leave-to {
  opacity: 0;
}
</style>
